package com.qf.fmw.service.impl;

import com.qf.fmw.dao.GoodsTypeDao;
import com.qf.fmw.dao.impl.GoodsTypeDaoImpl;
import com.qf.fmw.entity.bo.GoodsType;
import com.qf.fmw.entity.bo.ProductBo;
import com.qf.fmw.entity.vo.RespResult;
import com.qf.fmw.service.GoodsTypeService;

import java.util.Date;
import java.util.List;

public class GoodsTypeServiceImpl implements GoodsTypeService {
    private GoodsTypeDao goodsTypeDao = new GoodsTypeDaoImpl();

    // 查询所有商品分类
    @Override
    public RespResult showAllGoodsTypes() {
        List<GoodsType> goodsTypes = goodsTypeDao.selectAllGoodsTypes();
        if (goodsTypes == null || goodsTypes.isEmpty()) {
            return RespResult.respError("未找到商品分类", null);
        }
        return RespResult.respSuccess("找到商品分类", goodsTypes);
    }

    @Override
    public RespResult searchGoodsTypes(String level, String name) {
        List<GoodsType> goodsTypes = goodsTypeDao.searchGoodsTypes(level, name);
        if (goodsTypes == null || goodsTypes.isEmpty()) {
            return RespResult.respError("未找到符合条件的商品分类", null);
        }
        return RespResult.respSuccess("找到符合条件的商品分类", goodsTypes);
    }

    @Override
    public RespResult searchGoodsTypesByLevel(String level) {
        List<GoodsType> goodsTypes = goodsTypeDao.searchGoodsTypesByLevel(level);
        if (goodsTypes == null || goodsTypes.isEmpty()) {
            return RespResult.respError("未找到符合条件的商品分类", null);
        }
        return RespResult.respSuccess("找到符合条件的商品分类", goodsTypes);
    }

    @Override
    public RespResult searchGoodsTypesByName(String name) {
        List<GoodsType> goodsTypes = goodsTypeDao.searchGoodsTypesByName(name);
        if (goodsTypes == null || goodsTypes.isEmpty()) {
            return RespResult.respError("未找到符合条件的商品分类", null);
        }
        return RespResult.respSuccess("找到符合条件的商品分类", goodsTypes);
    }

    // 删除商品分类
    @Override
    public RespResult deleteGoodsType(int typeId) {
        int deleted = goodsTypeDao.deleteGoodsType(typeId);
        if (deleted == 0) {
            return RespResult.respError("删除失败", null);
        }
        return RespResult.respSuccess("删除成功", null);
    }

    @Override
    public RespResult showAllGoods() {
        List<ProductBo> goodsList = goodsTypeDao.showGoodsTypes();
        if (goodsList == null || goodsList.isEmpty()) {
            return RespResult.respError("未找到商品", null);
        }
        return RespResult.respSuccess("找到商品", goodsList);
    }

    @Override
    public RespResult searchGoods(String name, String pubdate) {
        List<ProductBo> goodsList = goodsTypeDao.searchGoods(name, pubdate);
        if (goodsList == null || goodsList.isEmpty()) {
            return RespResult.respError("未找到符合条件的商品", null);
        }
        return RespResult.respSuccess("找到符合条件的商品", goodsList);
    }

    //添加商品
    @Override
    public RespResult addGoods(String name, Date pubdate, String price, String star, String intro, String picture) {
        int result = goodsTypeDao.insertGoods(name, pubdate, price, star, intro, picture);
        if (result == 0) {
            return RespResult.respError("添加商品失败", null);
        }
        return RespResult.respSuccess("添加商品成功", null);
    }

    @Override
    public RespResult updateGoodsType(int typeId, String name, String level, String belongType) {
        int updated = goodsTypeDao.updateGoodsType(typeId, name, level, belongType);
        if (updated == 0) {
            return RespResult.respError("更新失败", null);
        }
        return RespResult.respSuccess("更新成功", null);

    }

    @Override
    public RespResult deleteGoods(int pId) {
        int deleted = goodsTypeDao.deleteGoods(pId);
        if (deleted == 0) {
            return RespResult.respError("删除失败", null);
        }
        return RespResult.respSuccess("删除成功", null);
    }



    // 更新商品
    @Override
    public RespResult updateGoods(int pId, String pName, String pPrice, String pTime, String pInfo) {
        int updated = goodsTypeDao.updateGoods(pId, pName, pPrice, pTime, pInfo);
        if (updated == 0) {
            return RespResult.respError("更新失败", null);
        }
        return RespResult.respSuccess("更新成功", null);
    }




    @Override
    public RespResult getGoodsInfo(int pId) {
        String info = goodsTypeDao.getGoodsInfo(pId);
        if (info == null || info.isEmpty()) {
            return RespResult.respError("未找到描述信息", null);
        }
        return RespResult.respSuccess("找到描述信息", info);
    }

}

